package Netty.protobuf;

import Netty.protobuf.protoClass.Message;
import com.alibaba.fastjson.JSON;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

public class NettyServerHandler extends SimpleChannelInboundHandler<Message.MessageProto> {

   @Override
   public void channelActive(ChannelHandlerContext ctx) throws Exception {
       System.out.println("Netty server 激活...");
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message.MessageProto msg) throws Exception {
        System.out.println("Netty server 接收消息："+ msg.toString());
        msg = msg.toBuilder().setContent("hello netty client").build();
        ctx.write(msg);
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

}